﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Magiq.Insert
{
    public class RepositoryQueryableProvider<TQueryable, TResult> : IInsertSourceQueryProvider
    {
        private readonly Expression<Func<TQueryable, IEnumerable<TResult>>> lambda;

        public RepositoryQueryableProvider(Expression<Func<TQueryable, IEnumerable<TResult>>> lambda)
        {
            this.lambda = lambda;
        }

        public IQueryable ApplyQueryTo(object source)
        {
            var typedSource = (TQueryable)source;
            
            var method = lambda.Compile();

            return (IQueryable) method(typedSource);
        }
    }
}